#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2021/12/22 16:25
@File: wsClient.py
"""
import time

import websocket

global ws

def on_message(ws, message):  # 服务器有数据更新时，主动推送过来的数据
    print(message)


def on_error(ws, error):  # 程序报错时，就会触发on_error事件
    print(error)


def on_close(ws):
    print("Connection closed")


def on_open(ws):  # 连接到服务器之后就会触发on_open事件，这里用于send数据
    req = 'python websocket client link!'
    print(req)
    ws.send(req)

def run_ws_client(url='ws://127.0.0.1:8001/markdown/ws/logs/Runlog/'):
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(url,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.on_open = on_open
    ws.run_forever(ping_timeout=30)
    return ws

if __name__ == '__main__':
    ws = run_ws_client()
    time.sleep(3)
    on_message(ws, 'www')
